package com.rules.admin.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.rules.admin.entity.Rule;
import com.rules.admin.entity.RuleVariable;
import com.rules.admin.service.RuleService;
import com.rules.admin.service.RuleVariableService;
import com.rules.admin.utils.ResultUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

@RequestMapping({"/rule"})
@Controller
public class RuleController {
   private static final Logger log = LoggerFactory.getLogger(RuleController.class);
   @Autowired
   private RuleService ruleService;
   @Autowired
   private RuleVariableService ruleVariableService;

   @RequestMapping(
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public IPage<Rule> queryPage(Long ruleAssemblyId, String name, IPage<Rule> page) {
      return this.ruleService.queryPage(ruleAssemblyId, name, page);
   }

   @RequestMapping(
      method = {RequestMethod.POST}
   )
   @ResponseBody
   public Map<String, Object> addRule(@RequestBody Rule rule) {
      List<Rule> rules = this.ruleService.getRuleListByAssemblyId(rule.getRuleAssemblyId());
      if (rules.stream().filter((currRule) -> {
         return currRule.getName().equals(rule.getName());
      }).count() > 0L) {
         return ResultUtils.getFaildResultData(new String[]{"name不能重复"});
      } else {
         int minPriority = 1;
         Iterator var4 = rules.iterator();

         while(var4.hasNext()) {
            Rule r = (Rule)var4.next();
            if (r.getPriority() < minPriority) {
               minPriority = r.getPriority();
            }

            r.setPriority(r.getPriority() + 1);
            this.ruleService.save(r);
         }

         rule.setPriority(minPriority);
         this.ruleService.save(rule);
         return ResultUtils.getSuccessResultData();
      }
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public Rule getRule(@PathVariable("id") Long id) {
      return this.ruleService.getById(id);
   }

   @RequestMapping(
      value = {"/getPriority"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public Integer getPriority(Long id) {
      Integer priority = 1;
      List<Rule> rules = this.ruleService.getRuleListByAssemblyId(id);
      if (rules.size() > 0) {
         Iterator var5 = rules.iterator();

         while(var5.hasNext()) {
            Rule newRule = (Rule)var5.next();
            if (newRule.getPriority() > priority) {
               priority = newRule.getPriority();
            }
         }

         return priority + 1;
      } else {
         return priority;
      }
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.PUT}
   )
   @ResponseBody
   public Map<String, Object> updateRule(@RequestBody Rule rule) {
      try {
         this.ruleService.update(rule);
      } catch (Exception var3) {
         log.error("保存失败！", var3);
         return ResultUtils.getFaildResultData();
      }

      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.DELETE}
   )
   @ResponseBody
   public Map<String, Object> deleteRule(@PathVariable("id") Long id) {
      List<RuleVariable> variableList = this.ruleVariableService.loadPackageVariables(3, id);
      if (!variableList.isEmpty()) {
         return ResultUtils.getFaildResultData(new String[]{"存在规则变量，请确认！"});
      } else {
         this.ruleService.removeById(id);
         return ResultUtils.getSuccessResultData();
      }
   }

   @RequestMapping(
      value = {"/copy"},
      method = {RequestMethod.POST}
   )
   @ResponseBody
   public Map<String, Object> copyRule(@RequestBody Rule rule) {
      int minPriority = 1;
      List<Rule> rules = this.ruleService.getRuleListByAssemblyId(rule.getRuleAssemblyId());
      Iterator var4 = rules.iterator();

      while(var4.hasNext()) {
         Rule r = (Rule)var4.next();
         if (r.getPriority() < minPriority) {
            minPriority = r.getPriority();
         }

         r.setPriority(r.getPriority() + 1);
         this.ruleService.save(r);
      }

      Rule newRule = this.ruleService.getById(rule.getId());
      newRule.setId((Long)null);
      newRule.setRuleAssemblyId(rule.getRuleAssemblyId());
      newRule.setName(rule.getName());
      newRule.setRemark(rule.getRemark());
      newRule.setPriority(minPriority);
      this.ruleService.save(newRule);
      return ResultUtils.getSuccessResultData();
   }
}
